﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using FShop_BAL;
using FShop_DAL;

public partial class Controls_Login : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtuser.Text = "";
            txtpassword.Text = "";
            if (Session["User_Name"] != null)//neu khach hang do da dang nhap
            {
                mtvdangnhap.ActiveViewIndex = 1; //thi hien view chao mung
                if (Session["User_Type"].Equals(2))//neu nguoi do chi co quyen khach hang....
                    pnadmin.Visible = true; //thi khong hien duong link cho phep vo trang admin...
                else
                    pnadmin.Visible = false;
            }
            else
                mtvdangnhap.ActiveViewIndex = 0;/// nguoc lai thi hien view dang nhap...
        }
    }
    
    protected void cmddangnhap_Click(object sender, EventArgs e)
    {
        string mem_ID = string.Empty;
        if (!User_BAL.IsExitUser(txtuser.Text.Trim(), CryptorEngine.Encrypt(txtpassword.Text.Trim(), true), ref mem_ID))
        {
            lblthongbao.Text = "Invalid username or password";
        }
        else
        {
            FShop_DAL.User mem = User_BAL.Get_User_ByID(int.Parse(mem_ID));
            if (mem != null)
            {
                Session["User_Type"] = mem.User_Type;
                Session["User_ID"] = mem.User_ID;
                Session["User_Name"] = mem.User_Name;
                if (Session["User_Name"] != null)//neu khach hang do da dang nhap
                {
                    mtvdangnhap.ActiveViewIndex = 1; //thi hien view chao mung
                    if (Session["User_Type"].Equals(2))//neu nguoi do co quyen admin....
                        pnadmin.Visible = true; //thi khong hien duong link cho phep vo trang admin...
                    else
                        pnadmin.Visible = false;
                }
                else
                    mtvdangnhap.ActiveViewIndex = 0;/// nguoc lai thi hien view dang nhap...
                //Response.Redirect("Default.aspx");                                                    
            }
        }
    }
    protected void lnkdangxuat_Click(object sender, EventArgs e)
    {
        Session["User_Type"] = null;
        Session["User_ID"] = null;
        Session["User_Name"] = null;
        txtuser.Focus();
        Response.Redirect("Default.aspx");
    }
}
